# 文字生成语言
import os
import appbuilder # pip install appbuilder-sdk 
# 在https://console.bce.baidu.com/ai_apaas/secretKey中创建
os.environ["APPBUILDER_TOKEN"] = "bce-v3/ALTAK-WEOV6K36WssEL2qASeebk/b6310e38332f6b39b4c59b1dddb37fa73c34d697"
tts_ab = appbuilder.TTS()

def tts(TEXT='你好啊', tts_wav_path = 'temp/tts.wav'):
    '''
    语音合成TTS，生成wav音频文件
    '''
    inp = appbuilder.Message(content={"text": TEXT})
    out = tts_ab.run(inp, model="paddlespeech-tts", audio_type="wav")
    # out = tts_ab.run(inp, audio_type="wav")
    with open(tts_wav_path, "wb") as f:
        f.write(out.content["audio_binary"])
    # print("TTS语音合成，导出wav音频文件至：{}".format(tts_wav_path))

# def play_wav(wav_file='temp/tts.wav'):
#     '''
#     播放wav音频文件
#     '''
#     prompt = 'aplay -t wav {} -q'.format(wav_file)
#     os.system(prompt)


if __name__ == '__main__':
    tts(TEXT='我是你的机器人管家，你可以喊我大白鲸')